package org.gruposp2p.proyectokarma.gwt.client.json;


import java.util.Set;
import org.gruposp2p.proyectokarma.gwt.client.domain.CometdMessage;
import org.gruposp2p.proyectokarma.gwt.client.domain.Document;
import org.gruposp2p.proyectokarma.gwt.client.domain.Peer;
import org.gruposp2p.proyectokarma.gwt.client.util.DateUtils;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import static org.gruposp2p.proyectokarma.gwt.client.json.JSONConstants.*;

public class DomainToJSON {
	
    public static JSONObject getCometdMessage (CometdMessage cometdMessage) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MESSAGE_GROUP, new JSONString(cometdMessage.group));
        jsonObject.put(MESSAGE_ACTION, new JSONString(cometdMessage.action));
        jsonObject.put(MESSAGE_CONTENT, new JSONString(cometdMessage.content));
        jsonObject.put(MESSAGE_PASSWORD, new JSONString(cometdMessage.password));
        jsonObject.put(MESSAGE_PEER_FROM, new JSONString(cometdMessage.peerFrom));
        jsonObject.put(MESSAGE_PEER_TO, new JSONString(cometdMessage.peerTo));
        jsonObject.put(MESSAGE_SUBJECT, new JSONString(cometdMessage.subject));
        return jsonObject;
    }

    public static JSONObject getDocument (Document document) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(DOCUMENT_NAME, new JSONString(document.getName()));
        jsonObject.put(DOCUMENT_CONTENT, new JSONString(document.getContent()));
        jsonObject.put(DOCUMENT_IS_POLLING, JSONBoolean.getInstance(document.isPolling()));
        jsonObject.put(DOCUMENT_POLLING_DATE_TO, new JSONString(
        		DateUtils.getSimpleStringFromDate(document.getPollingValidTo())));
        if (document.getTags() != null) {
        	Set<String> tags = document.getTags();
        	JSONArray jsonArray = new JSONArray ();
        	int index = 0;
        	for (String tag : tags) {
        		jsonArray.set(index++, new JSONString(tag));
        	}
        	jsonObject.put(DOCUMENT_TAGS, jsonArray);
        }
        if (document.getOptions() != null) {
        	Set<String> options = document.getOptions();
        	JSONArray jsonArray = new JSONArray ();
        	int index = 0;
        	for (String option : options) {
        		jsonArray.set(index++, new JSONString(option));
        	}
        	jsonObject.put(DOCUMENT_OPTIONS, jsonArray);
        }
        return jsonObject;
    }
    
    public static JSONObject getPeer (Peer peer) {
        JSONObject jsonObject = new JSONObject();
        if (peer.getName() != null)
        	jsonObject.put(PEER_NAME, new JSONString(peer.getName()));
        if (peer.getUsername() != null)
        	jsonObject.put(PEER_USERNAME, new JSONString(peer.getUsername()));
        if (peer.getEmail() != null)
        	jsonObject.put(PEER_EMAIL, new JSONString(peer.getEmail()));
        if (peer.getPassword() != null)
        	jsonObject.put(PEER_PASSWORD, new JSONString(peer.getPassword()));
        if (peer.isAccountExpired() != null)
        	jsonObject.put(PEER_ACCOUNT_EXPIRED, JSONBoolean.getInstance(peer.isAccountExpired()));
        if (peer.isAccountLocked() != null)
        	jsonObject.put(PEER_ACCOUNT_LOCKED, JSONBoolean.getInstance(peer.isAccountLocked()));
        if (peer.isPasswordExpired() != null)
        	jsonObject.put(PEER_PASSWORD_EXPIRED, JSONBoolean.getInstance(peer.isPasswordExpired()));
        if (peer.isEnabled() != null)
        	jsonObject.put(PEER_ENABLED, JSONBoolean.getInstance(peer.isEnabled()));
        return jsonObject;
    }
}